// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
 * https://github.com/adamped/xamarin.flutter/blob/566ec22f96243b7e01faa57fec6f4b540564c9d0/flutter/test/cupertino/tab_test.dart
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Use home', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
          return new CupertinoPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabView(
                builder: (BuildContext context) => const Text('home'),
              );
            },
          );
        },
      ),
    );

    expect(find.text('home'), findsOneWidget);
  });

  testWidgets('Use routes', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
          return new CupertinoPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabView(
                routes: <String, WidgetBuilder>{
                  '/': (BuildContext context) => const Text('first route'),
                },
              );
            },
          );
        },
      ),
    );

    expect(find.text('first route'), findsOneWidget);
  });

  testWidgets('Use home and named routes', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
          return new CupertinoPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabView(
                builder: (BuildContext context) {
                  return new CupertinoButton(
                    child: const Text('go to second page'),
                    onPressed: () {
                      Navigator.of(context).pushNamed('/2');
                    },
                  );
                },
                routes: <String, WidgetBuilder>{
                  '/2': (BuildContext context) =>
                      const Text('second named route'),
                },
              );
            },
          );
        },
      ),
    );

    expect(find.text('go to second page'), findsOneWidget);
    await tester.tap(find.text('go to second page'));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 300));
    expect(find.text('second named route'), findsOneWidget);
  });

  testWidgets('Use onGenerateRoute', (WidgetTester tester) async {
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
          return new CupertinoPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabView(
                onGenerateRoute: (RouteSettings settings) {
                  if (settings.name == Navigator.defaultRouteName) {
                    return new CupertinoPageRoute<void>(
                        settings: settings,
                        builder: (BuildContext context) {
                          return const Text('generated home');
                        });
                  }
                },
              );
            },
          );
        },
      ),
    );

    expect(find.text('generated home'), findsOneWidget);
  });

  testWidgets('Use onUnknownRoute', (WidgetTester tester) async {
    String unknownForRouteCalled;
    await tester.pumpWidget(
      new WidgetsApp(
        color: const Color(0xFFFFFFFF),
        onGenerateRoute: (RouteSettings settings) {
          return new CupertinoPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              return new CupertinoTabView(
                onUnknownRoute: (RouteSettings settings) {
                  unknownForRouteCalled = settings.name;
                },
              );
            },
          );
        },
      ),
    );

    expect(tester.takeException(), isFlutterError);
    expect(unknownForRouteCalled, '/');
  });
}
